package com.itheima.d4_threadsychronized;

public class Account {
    private String cardId;//卡号
    private  double money;//余额

    public Account() {
    }
    //  构造器 分别是cardId 和 余额
    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    /**
     * 获取
     * @return cardId
     */
    public String getCardId() {
        return cardId;
    }

    /**
     * 设置
     * @param cardId
     */
    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    /**
     * 获取
     * @return money
     */
    public double getMoney() {
        return money;
    }

    /**
     * 设置
     * @param money
     */
    public void setMoney(double money) {
        this.money = money;
    }
    //小明小红都到这里来取钱

    //同步方法 直接在修饰符前面加上synchronized
    public synchronized void drawMoney(double money){

        //获取到当前线程的名称
        String name = Thread.currentThread().getName();
        //判断当前余额是否充足
        if(this.money >= money){
            //余额充足
            System.out.println(name + "取钱成功，取钱金额：" + money);
            //更新余额
            this.money -= money;
            System.out.println(name + "取钱成功，剩余金额：" + this.money);
        }else{
            //余额不足
            System.out.println(name + "取钱失败，余额不足！");
        }

    }

}
